<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="page-search layui-form" lay-filter="search-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input name="search-key" placeholder="请输入姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="search-submit">查询</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn create-btn">新建</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="table" lay-filter="table"></table>
<div id="pagination"></div>

</body>
<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-xs" lay-event="update">更新</button>
    {{#  if(d.disable){ }}
    <input type="checkbox" name="status" lay-skin="switch" data-device-id={{d.id}} lay-filter="status">
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" checked data-device-id={{d.id}} lay-filter="status">
    {{#  } }}
</script>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['table', 'form', 'laypage', 'conf', 'utils', 'mandyServices''], function (table, form, laypage, config, utils, mandyServices) {
        var $createBtn = $('.create-btn');
        utils.aresPageEventInit('updateDeviceList', 'device', function () {
            getPageList(1, config.pagination.limit);
        })

        getPageList(1, config.pagination.limit);
        form.on('submit(search-submit)', function (data) {
            getPageList(1, config.pagination.limit);
            return false;
        });

        //启用禁用
        form.on('switch(status)', function (data) {
            enableProfile($(data.elem).data('deviceId'), data.elem.checked).catch(function (err) {
                data.elem.checked = !data.elem.checked;
                form.render();
            })
        });

        $createBtn.on('click', function () {
            showPersonModal('新建设备')
            return false;
        })

        function showPersonModal(title, id) {
            var queryStr = (typeof id == 'string' || typeof id == 'number') ? '?id=' + id : '';
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/system/device_form.html' + queryStr,
                end: function (index, layero) {
                    utils.ARES.cache.device = null;
                }
            });
        }

        function handlerUpdate(data) {
            utils.ARES.cache.device = data;
            showPersonModal('更新设备', data.id);
        }

        //获取设备列表
        function getPageList(currentPage, pageSize) {
            var searchKey = $('input[name="search-key"]').val();
            mandyServices.mandy_device_list_by_page(searchKey, currentPage, pageSize).then(function (res){
                var data = res.data;
                laypage.render($.extend(config.pagination, {
                    count: data.count,
                    curr: data.current_page,
                    jump: function (obj, first) {
                        if (!first) getPageList(obj.curr, obj.limit);
                    }
                }));
                table.render({
                    elem: '#table',
                    data: data.list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '设备类型', templet: '<span>{{d.device_type.alias}}</span>'},
                        {title: '设备名称', field: 'name'},
                        {title: '负责人', field: 'director'},
                        {title: '可同时治疗人数', field: 'treatPeople'},
                        {title: '治疗项目', field: 'subitem_name',width: 300,},
                        {title: '价格(元)', field: 'unitFee'},
                        {title: '操作', fixed: 'right', width: 100, templet: '#tableBar'}
                    ]]
                });
                table.on('tool(table)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'update':
                            handlerUpdate(data)
                            break;
                    }
                    ;
                });
            })
        }

        //启用禁用设备
        function enableProfile(deviceId, checked) {
            mandyServices.mandy_put_device_disabled(deviceId, !checked).then(function (res) {
                utils.msgSuccess(res.message);
            });
        }

    })
</script>


</html>